package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.Category;
import com.course.server.domain.CategoryExample;
import com.course.server.dto.CategoryDto;
import com.course.server.mapper.CategoryMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     */

    public List<CategoryDto> all(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<CategoryDto> categoryDtoList = CopyUtil.copyList(categoryList, CategoryDto.class);
        return categoryDtoList;
    }

//    public void list(PageDto pageDto){
//        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
//        CategoryExample categoryExample = new CategoryExample();
//        categoryExample.setOrderByClause("sort asc");
//        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
//        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
//        pageDto.setTotal(pageInfo.getTotal());
//        List<CategoryDto> categoryDtoList = CopyUtil.copyList(categoryList, CategoryDto.class);
//        pageDto.setList(categoryDtoList);
//    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    public void save(CategoryDto categoryDto){
        Category category = CopyUtil.copy(categoryDto, Category.class);
        if(StringUtils.isEmpty(categoryDto.getId())){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(category);
        }else {
            this.update(category);
        }
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(Category category){
        category.setId(UuidUtil.getShortUuid());
        categoryMapper.insert(category);
    }
    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(Category category){
        categoryMapper.updateByPrimaryKey(category);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    @Transactional
    public void delete(String id){
        deleteChildren(id);
        categoryMapper.deleteByPrimaryKey(id);
    }
    /**
     * create by: lin
     * description: 删除子分类
     * create time: 2020/8/2 18:09
     */
    public void  deleteChildren(String id){
        Category category = categoryMapper.selectByPrimaryKey(id);
        if("00000000".equals(category.getParent())){
            //如果是一级分类，需要删除其下的二级分类
            CategoryExample example = new CategoryExample();
            example.createCriteria().andParentEqualTo(category.getId());
            categoryMapper.deleteByExample(example);
        }
    }
}
